package zy.blue7.gui.netty.custom;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @Author: blue7
 * @Date: 2022/2/27 10:42
 * @Version 1.0
 * @Description
 */
@ChannelHandler.Sharable
public class Blue7MessageEncoder extends MessageToByteEncoder<Blue7Message> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Blue7Message blue7Message, ByteBuf byteBuf) throws Exception {

        /**
         * 获取请求头信息
         */
        Blue7Header blue7Header = blue7Message.getBlue7Header();

        /**
         * 写入Header信息
         */
        byteBuf.writeInt(blue7Header.getVersion());
        byteBuf.writeInt(blue7Header.getContentLength());
        byteBuf.writeBytes(blue7Header.getSessionId().getBytes());

        /**
         * 写入消息主体信息
         */
        byteBuf.writeBytes(blue7Message.getContent().getBytes());



    }
}
